import java.util.NoSuchElementException;
import java.util.Scanner;

import java.sql.Date;
import java.sql.Time;


public final class AppointmentMessageEditor {
	
	/**
	 * This variable is the juice of the break.
	 */
	private static final String breakSign = MessageEditor.breakSign;
	
	/**
	 * This variable needs to have at least one space on each side of it ex: " breakSign "
	 */
	private static final String dBreak = " " + breakSign + " ";
	
	public static Appointment getAppointment(String message){
		Scanner scan = new Scanner(message);
		
		try{
			String pre = scan.next();
			
			if(pre.equals("APPOINTMENT")){
				/* When the message indicated that the appointment was not null */
				/* 
				 * clears off the first dBreak - checks to make sure that 
				 * there are dBreaks where they should be.
				 */
				if(!scan.next().equals(breakSign)) return null;

				/*
				 * Gets the data!
				 */
				int ssn = Integer.parseInt(scan.next());
				if(!scan.next().equals(breakSign)) return null;

				Date date = Date.valueOf(scan.next());
				if(!scan.next().equals(breakSign)) return null;
				
				Time ts = Time.valueOf(scan.next());
				if(!scan.next().equals(breakSign)) return null;
				
				Time te = Time.valueOf(scan.next());
				if(!scan.next().equals(breakSign)) return null;
				
				/* Need to loop to get all of the parts of these ;*/
				String current;
				String reason = "";
				while(!(current = scan.next()).equals(breakSign)){
					reason += current + " ";
				}

				String doctor = "";
				while(!(current = scan.next()).equals(breakSign)){
					doctor += current + " ";
				}

				String patient = "";
				while(!(current = scan.next()).equals(breakSign)){
					patient += current + " ";
				}

				/* This checks if this is the end of the message */
				if(!scan.next().equals("END")) return null;

				/*
				 * Creates the Appointment Object
				 */
				Appointment app = new Appointment(ssn, date, ts, te, doctor, patient);
				app.setReason(reason);
				
				return app;
				
			}else if(pre.equals("NULL")){
				/* When the message indicated that the appointment was null */
				return null;
				
			}else{
				/* When the message was incorrect */
				return null;
			}
			
		}catch(NoSuchElementException e){
			return null;
			
		}catch(NumberFormatException e){
			return null;
		}
		
	}
	
	public static String getMessage(Appointment appointment){
		if(appointment == null){
			return "NULL" + dBreak + "END";
		}else{
			String app = "APPOINTMENT" + dBreak + 
						 appointment.getSSN() + dBreak;
			
			Date date = appointment.getDate();
			app += date.toString() + dBreak;
			
			Time ts, te;
			ts = appointment.getTimeStart();
			te = appointment.getTimeEnd();
			app += ts.toString() + dBreak +
				   te.toString() + dBreak;
			
			app += appointment.getReason() + dBreak +
				   appointment.getDoctor() + dBreak +
				   appointment.getPatient() + dBreak;
			
			app += "END";
			
			return app;
		}
	}
	
	/* MAIN METHOD TEST
	public static void main(String[] args){
		Date date = new Date(111, 4, 12);
		Time ts = new Time(3, 12, 35);
		Time te = new Time(3, 38, 12);
		Appointment app = new Appointment(678, date, ts, te, "Doc John", "Susie");
		app.setReason("My tooth hurts!!!!!");
		
		String msg = getMessage(app);
		System.out.println(msg);
		
		Appointment appointment = getAppointment(msg);
		System.out.println(appointment.getSSN());
		System.out.println(appointment.getDate());
		System.out.println(appointment.getTimeStart());
		System.out.println(appointment.getTimeEnd());
		System.out.println(appointment.getReason());
		System.out.println(appointment.getDoctor());
		System.out.println(appointment.getPatient());
	}*/
	
}
